<?php

namespace App\Http\Controllers\Main;

use App\Model\Contact;
use App\Model\Partner;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Model\Config;
use App\Model\ServiceCategory;
use App\Model\Service;
use App\Model\News;
use App\Model\Idea;
use Illuminate\Support\Facades\Input;

class IndexController extends Controller
{
    //

    public function index() {
        #获取服务分类
        $service = ServiceCategory::select('id','name')->where('status',1)->orderBy('rank','desc')->get()->toArray();
        foreach ($service as $key => $value) {
            #获取推荐的服务文章
            $service[$key]['recomm'] = Service::select('id','title','picture')->where('category_id',$value['id'])->where('recomm',1)->where('status',1)->orderBy('rank','desc')->take(6)->get()->toArray();
            #获取置顶的服务文章
            $service[$key]['top'] = Service::select('id','title','picture','description')->where('category_id',$value['id'])->where('top',1)->first();
        }
//        dump($service);
        $news_top = News::select('id','title','picture','description')->where('top',1)->where('status',1)->first();
        $news_recomm = News::select('id','title','picture')->where('recomm',1)->where('status',1)->orderBy('rank','desc')->take(3)->get();
        $news = News::select('id','title','picture','create_time','make_time')->where('recomm',0)->where('top',0)->where('status',1)->orderBy('rank','desc')->take(6)->get();
        foreach ($news as $new => $val) {
            $news[$new]['create'] = substr($val['create_time'],0,10);
//            $news[$new]['title1'] = substr($val['title'],0,10);
        }

        $partner = Partner::where('status',1)->orderBy('rank','desc')->take(12)->get();
//        dump($news);
        return view('main/index',compact('service','news','news_top','news_recomm','partner'));
    }

    public function idea($id) {
        $data = Idea::find($id);
        $data->create = substr($data->create_time,0,10);
        return view('main.idea',compact('data'));
    }

    public function search() {
        $data = array(); $data1 = array(); $data2 = array();
        $keywords = Input::get('keywords');
        if(empty($keywords)) {
            return redirect()->back()->withInput()->withErrors('关键词不能为空');
        }
        $data1 = News::select('id','title','picture','create_time')->where('title','like',"%$keywords%")->where('status',1)->orderBy('create_time','desc')->get();
        $data2 = Service::select('id','title','picture','create_time')->where('title','like',"%$keywords%")->where('status',1)->orderBy('create_time','desc')->get();
        foreach ($data1 as $new => $val) {
            $data1[$new]['create'] = substr($val['create_time'],0,10);
        }
        foreach ($data2 as $service => $value) {
            $data2[$service]['create'] = substr($value['create_time'],0,10);
        }
//        $data = array_merge($data1,$data2);
//        dump();
        return view('main.search',compact('data1','data2'));
    }

    public function partner_more() {
        $partner = Partner::where('status',1)->orderBy('rank','desc')->get();
        return view('main.partner',compact('partner'));
    }
}
